# Contributor: Antoine Martin (ayakael) <dev@ayakael.net>
# Maintainer: Antoine Martin (ayakael) <dev@ayakael.net>
pkgname=powershell
pkgver=7.4.6
pkgrel=2
pkgdesc="A cross-platform automation and configuration tool/framework"
arch="x86_64 aarch64 armv7" # blocked by dotnet runtime
url="https://github.com/PowerShell/PowerShell"
license="MIT"
depends="
	dotnet8-runtime
	libpsl-native
	"
makedepends="
	dotnet8-sdk
	git
	"
source="
	$pkgname-$pkgver.tar.gz::https://github.com/PowerShell/PowerShell/archive/v$pkgver.tar.gz
	add-missing-nuget-src.patch
	rollback-system-manage-automation-psversioninfo.patch
	dependency-gatherer.targets
	fix-filesystem-test.patch
	"
builddir="$srcdir/PowerShell-$pkgver"
# if true, then within pipeline environment, in which case send logs there
# to be scooped
if [ -d "$APORTSDIR/logs" ]; then
	_logdir="$APORTSDIR"/logs
else
	_logdir="$srcdir"/logs
fi

# mono-flavored runtime does not support crossgen2
case $CARCH in
	armhf|s390x|ppc64le) _use_crossgen2=false;;
esac

# change nuget cache
export NUGET_PACKAGES="$srcdir"/nuget_cache

# secfixes:
#   7.3.10-r0:
#     - CVE-2023-36013

prepare() {
	default_prepare

	# build expects to be in git directory
	git init
	git config user.name "example"
	git config user.email "example@example.com"
	git add -f ./*
	git commit -m 'Initial'
	git tag -a "v$pkgver" -m "Initial"
	git remote add origin "$url"
	rm "global.json"
	mkdir -p "$_logdir"

	# Replicating build.psm1:Start-PSBuild()
	## Restore-PSPackage()
	dotnet restore --locked-mode -p:PublishReadyToRun=$_use_crossgen2 --use-current-runtime src/powershell-unix
	dotnet restore --locked-mode src/TypeCatalogGen
	dotnet restore --locked-mode src/ResGen
	dotnet restore --locked-mode src/Modules
	dotnet restore --locked-mode src/Microsoft.PowerShell.GlobalTool.Shim
}

build() {
	msg "Building $pkgname"

	## Start-ResGen(): Generate resource binding C# files
	(
		cd "$builddir"/src/ResGen
		dotnet run --no-restore
	)

	## Start-TypeGen(): Setup the build target to gather dependency information
	cp "$srcdir/dependency-gatherer.targets" \
		"src/Microsoft.PowerShell.SDK/obj/Microsoft.PowerShell.SDK.csproj.TypeCatalog.targets"

	dotnet msbuild \
		src/Microsoft.PowerShell.SDK \
		-t:_GetDependencies \
		-p:DesignTimeBuild=true \
		-p:_DependencyFile="$builddir"/src/TypeCatalogGen/powershell.inc \
		-nologo

	dotnet run \
		--no-restore \
		--project src/TypeCatalogGen \
		src/System.Management.Automation/CoreCLR/CorePsTypeCatalog.cs \
		"$builddir"/src/TypeCatalogGen/powershell.inc

	## Build powershell core
	dotnet build-server shutdown
	dotnet publish --configuration Linux "src/powershell-unix/" \
		--output lib \
		--no-self-contained \
		--no-restore \
		--framework "net8.0" \
		--use-current-runtime \
		-p:PublishReadyToRun=$_use_crossgen2 \
		/bl:"$_logdir"/powershell-build.binlog \
		/consoleLoggerParameters:ShowTimestamp

	## Publish reference assemblies
	grep 'Microsoft.NETCore.App' "$builddir"/src/TypeCatalogGen/powershell.inc | sed 's/;//' | while read -r assembly; do
		install -Dm755 -t lib/ref "$assembly"
	done

	# powershell doesn't build all standard libraries,
	# emulate upstream build behavior
	# see https://github.com/PowerShell/PowerShell/issues/20143
	_missing_libs="
		PackageManagement PowerShellGet Microsoft.PowerShell.Archive
		PSReadLine ThreadJob
	"
	for lib in $_missing_libs; do
		cp -ar "$NUGET_PACKAGES"/"$(echo $lib | tr '[:upper:]' '[:lower:]' )"/* "$builddir"/lib/Modules/$lib
	done
}

check() {
	msg "Checking $pkgname"
	cd "$builddir"/test/xUnit
	dotnet build-server shutdown
	dotnet test \
		-c Release \
		--runtime "$(dotnet --info | awk '$1=="RID:"{print $2}')" \
		-p:PublishReadyToRun=$_use_crossgen2 \
		/bl:"$_logdir"/powershell-check.binlog \
		/consoleLoggerParameters:ShowTimestamp
}

package() {
	# directory creation
	install -dm 755 \
		"$pkgdir"/usr/lib \
		"$pkgdir"/usr/bin

	# libary copy
	cp -ar "$builddir"/lib "$pkgdir"/usr/lib/$pkgname

	# already provided by 'libpsl-native' aport
	rm -f "$pkgdir"/usr/lib/$pkgname/libpsl-native.so

	# does not build for linux-musl, thus points to libc.so rather than musl
	# see https://github.com/dotnet/runtime/issues/63187
	rm -f "$pkgdir"/usr/lib/$pkgname/libSystem.IO.Ports.Native.so

	# binary link
	ln -s "/usr/lib/$pkgname/pwsh" "$pkgdir"/usr/bin/pwsh
}

sha512sums="
6e25042aef9598ba4bb54613aa86bbbde70bf71ffa0bf15571d493889fd26ab1d5bbaec35b244b5793368b7e6f1ef0731aad3f6be87d2ac650fe6ab137b1fdf9  powershell-7.4.6.tar.gz
294cad2568c1442fcc26d6a277d59d8c3da362f4b4c12d61e8a228cc830e04516dabfb7b965c927fdd50ca57d98c89016b900f44a67be259bf0ccb45e9808667  add-missing-nuget-src.patch
bac5ac577fd8e6bb8ba83b5c537269b635619ecde2dfa3b31de754865aab8f4fd004bcb972f08f3218ecbcb7ef33c7fb8489370f7f0aaa0bae2e88e3ec736625  rollback-system-manage-automation-psversioninfo.patch
5a70efe247cdae8ed5c66702909137e0727cf5e25aca54983891fc17716a56081825f9a6e57c56f8fa8aa5615a159f81c68ca501d42e2ce085c073be633b3025  dependency-gatherer.targets
0e7550d8b702eeb9ebbd42100b32cbf7a0be676c5922cd6ee03ecdd3ba11d20d42a468f73ee428c6c92c8b7b18f9b022591a368191c2a358034ba4d7e78d2b84  fix-filesystem-test.patch
"
